﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Meraz.Core.Model;
using System.Text;

namespace Meraz.Web.Views.Helpers
{
    public static class TwitterHelper
    {
        public static string TwitterUserListItem(this HtmlHelper html, string studyID, string username)
        {
            return TwitterUserListItem(studyID, username);
        }
        public static string TwitterUserListItem(string studyID, string username)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<li>");
            builder.Append(@"<button class='delete' type='button' onclick='window.PageBus.publish(TwitterStudy.remove, {username: """);
            builder.Append(username);
            builder.Append(@""", studyID: """);
            builder.Append(studyID);
            builder.Append(@""", source: this});'>Remove</button>");
            builder.AppendFormat(@"<a href='./{0}/{1}'>{1}</a>", username, studyID);
            builder.AppendLine();
            builder.AppendLine("</li>");
            return builder.ToString();
        }

        public static string SelectList(this HtmlHelper html, IEnumerable<Option> options, string name, bool isMultiple)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("<select id='{0}'", name);
            if (isMultiple)
                builder.Append(" multiple='true'");
            builder.AppendLine(">");
            foreach (Option option in options)
            {
                builder.AppendFormat("<option value='{0}'", option.Value);
                if (option.IsSelected)
                    builder.Append(" selected='true'");
                builder.AppendFormat(">{0}</option>", option.Text);
                builder.AppendLine();
            }
            builder.AppendLine("</select>");
            return builder.ToString();
        }
        public static string SelectList(this HtmlHelper html, IEnumerable<Option> options, string name, bool isMultiple, string selection)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("<select id='{0}'", name);
            if (isMultiple)
                builder.Append(" multiple='true'");
            builder.AppendLine(">");
            foreach (Option option in options)
            {
                builder.AppendFormat("<option value='{0}'", option.Value);
                if (option.Value == selection || option.Text == selection)
                    builder.Append(" selected='true'");
                builder.AppendFormat(">{0}</option>", option.Text);
                builder.AppendLine();
            }
            builder.AppendLine("</select>");
            return builder.ToString();
        }

        public static string UserTableRow(this HtmlHelper html, TwitterUser user)
        {
            return UserTableRow(user);
        }

        public static string UserTableRow(TwitterUser user)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<td>");

            builder.Append(BuildButton("edit", "TwitterUsersHandler.edit", user.ID.ToString(), "Details"));
            builder.Append(BuildButton("delete", "TwitterUsersHandler.remove", user.ID.ToString(), "Remove"));
            builder.AppendFormat(@"<a class='link' href='http://twitter.com/{0}' target='_blank'>Link</a>", user.Name);
            builder.AppendFormat(@"<a class='open' href='/Twitter/Tweets/{0}'>Search</a>", user.Name);



            builder.AppendLine("</td>");
            builder.AppendLine(string.Format("<td class='UserName'>{0}</td>", user.Name));
            builder.AppendLine();
            builder.AppendFormat("<td class='IsBlog'>{0}</td>", user.IsBlog);
            builder.AppendLine();
            builder.AppendFormat("<td class='IsNewspaper'>{0}</td>", user.IsNewspaper);
            builder.AppendLine();
            builder.AppendFormat("<td class='IsTopJournalist'>{0}</td>", user.IsTopJournalist);
            builder.AppendLine();
            builder.AppendFormat("<td class='IsTopPolitical'>{0}</td>", user.IsTopPolitical);
            builder.AppendLine();
            return builder.ToString();
        }
        static string BuildButton(string iconClass, string eventName, string data, string label)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"<button class='{0}' ", iconClass);
            builder.AppendFormat("value='{0}' ", data);
            builder.AppendFormat(@"onclick='window.PageBus.publish({0}, this)' ", eventName);
            builder.Append(">");
            builder.Append(label);
            builder.Append("</button>");
            return builder.ToString();
        }

        static string BuildCheckBox(string name, string key, string label, bool isChecked)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("<input type='checkbox' id='{0}-{1}' name='{0}-{1}' ", name, key);
            if (isChecked)
                builder.Append("checked='true' ");
            builder.AppendFormat("/><label for='{0}-{1}'>{2}</label>", name, key, label);


            return builder.ToString();
        }

        public static string BuilderToneBoxes(this HtmlHelper html, string name, string value)
        {
            return BuilderToneBoxes(name, value);
        }
        static string BuilderToneBoxes(string name, string value)
        {
            string[] values = { "Positive", "Negative", "Neutral"};
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < values.Length; i++)
            {
                builder.AppendFormat(@"<input type='radio' id='{0}-{1}' name='{0}' value='{2}' class='{2}' ", name, i, values[i]);
                if (value == values[i])
                    builder.Append("checked='true' ");
                builder.AppendFormat(@"/><label for='{0}-{1}'>{2}</label>", name, i, values[i]);
                builder.AppendLine();
            }
            return builder.ToString();
        }


        static string BuildTHead(IEnumerable<string> headings)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<thead>");
            foreach (string heading in headings)
            {
                builder.AppendFormat("<th>{0}</th>", heading);
            }
            builder.Append("</thead>");
            return builder.ToString();
        }


        public static string StatusList(string status)
        {
            string[] statusNames = { "Active", "Done", "Review" };
            StringBuilder builder = new StringBuilder();
            builder.AppendLine();
            for (int i = 0; i < statusNames.Length; i++)
            {
                string name = statusNames[i];
                builder.AppendFormat(@"<input type='radio' id='Status-{0}' name='Status' value='{1}'", i, name);
                if (name.Equals(status))
                    builder.Append(" checked='true'");
                builder.Append(" />");
                builder.AppendFormat(@"<label for='Status-{0}'>{1}</label>", i, name);
                builder.AppendLine();
            }
            return builder.ToString();
        }

        public static string EnumList<T>(int index, string selected)
        {
            StringBuilder builder = new StringBuilder();
            string typeName = typeof(T).Name;
            string selectboxName = typeName + "-" + index;
            builder.AppendFormat("<select id='{0}' name='{0}' class='{1}'>", selectboxName, typeName);
            builder.AppendLine();
            foreach (string name in Enum.GetNames(typeof(T)))
            {
                builder.AppendFormat("<option value='{0}'", name);
                if (name == selected)
                    builder.Append(" selected='true'");
                builder.AppendFormat(">{0}</option>", name);
                builder.AppendLine();
            }
            builder.AppendLine("</select>");
            return builder.ToString();
        }
        public static string EnumList<T>(string selected)
        {
            StringBuilder builder = new StringBuilder();
            string typeName = typeof(T).Name;
            string selectboxName = typeName;
            builder.AppendFormat("<select id='{0}' name='{0}' class='{1}'>", selectboxName, typeName);
            builder.AppendLine();
            foreach (string name in Enum.GetNames(typeof(T)))
            {
                builder.AppendFormat("<option value='{0}'", name);
                if (!string.IsNullOrWhiteSpace(selected) && name == selected)
                    builder.Append(" selected='true'");
                builder.AppendFormat(">{0}</option>", name);
                builder.AppendLine();
            }
            builder.AppendLine("</select>");
            return builder.ToString();
        }

    }
}